﻿var num = 42;
var itemPerPage = 9;
var page = 1;

var from = 1;
var to=9;

NextPage = function()
{
	if(Math.ceil(num/itemPerPage) > page)
		GoPage(page+1);
}

PrevPage = function()
{
	if(page > 1)
		GoPage(page-1);
}

HideButton = function()
{
	if(page == 1)
	{
		if($("#prev").attr("class").indexOf("disable") == -1)
			$("#prev").attr("class",$("#prev").attr("class").replace('icon-prev', 'icon-prev-disable'));
	}
	else
	{
		if($("#prev").attr("class").indexOf("disable") != -1)
			$("#prev").attr("class",$("#prev").attr("class").replace('icon-prev-disable', 'icon-prev'));
	}
	if(Math.ceil(num/itemPerPage) > page)
	{
		if($("#next").attr("class").indexOf("disable") != -1)
			$("#next").attr("class",$("#next").attr("class").replace('icon-next-disable', 'icon-next'));
	}
	else
	{
		if($("#next").attr("class").indexOf("disable") == -1)
			$("#next").attr("class",$("#next").attr("class").replace('icon-next', 'icon-next-disable'));
	}
	
	$("#page").text(page);
	if(from < to)
		$("#record").text("ลำดับ " + from + "-" + to + " จาก " + num);
	else
		$("#record").text("ลำดับ " + to + "-" + to + " จาก " + num);
}

GoPage = function(pageId)
{
	page = pageId;
	if(Math.ceil(num/itemPerPage) < pageId)
		$("#page").text("Cannot Navigate");
	else
	{
		from = ((pageId-1) * itemPerPage + 1);
		to = (itemPerPage * pageId);
		if(to > num)
			to = num;
		
		SearchByName();
	}
	HideButton();
}
callLoad = function () {
SearchByNameAll();
};

window.onload = callLoad;
